<?php

if(!isset($_SESSION['username'])){
		echo "<script>alert('请登录！');window.location.href='index.php?m=admin&c=manage'</script>";die;
	}


	//权限管理
	authonity($con);

	//权限角色列表查询
	$permission = dbselect($con,'permission');

	assign('permission',$permission);


	//ajax
	if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == "xmlhttprequest"){
		if(isset($_POST['uname'])){

			$uname = htmlspecialchars(trim($_POST['uname'],''));

			$query = dbselect($con,'yonghu','username',"username='{$uname}'");

			$unamerule = "/^[a-zA-z]+[\w_]{7,17}$/";

			


			if($query){
				$uun = 1 ;
			}else{
				$uun = 0 ;
			}
			$list['urule'] = preg_match($unamerule,$uname);

			$list['uuniqueness'] = $uun;

			echo json_encode($list);die;

		}else if(isset($_POST['ncheng'])){
			$ncheng = htmlspecialchars(trim($_POST['ncheng'],''));

			$query = dbselect($con,'yonghu','username',"nicheng='{$ncheng}'");

			if($query){
				echo 1 ;die;
			}else{
				echo 0 ;die;
			}
		}else if(isset($_POST['password'])){

			$bpassword = $_POST['password'];

			$passrule = "/^[\w_]{6,10}$/";

			echo preg_match($passrule, $bpassword);die;	

		}else if(isset($_POST['phone'])){
			$phone = htmlspecialchars(trim($_POST['phone'],''));

			$query = dbselect($con,'yonghu','phone',"phone='{$phone}'");

			$phonerule = "/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/";

			if($query){

				$pun = 1 ;
			}else{
				$pun = 0 ;
			}

			

			$list['prule'] = preg_match($phonerule,$phone);

			$list['puniqueness'] = $pun;	

			echo json_encode($list);die;



		}else if(isset($_POST['email'])){
			$email = htmlspecialchars(trim($_POST['email'],''));

			$query = dbselect($con,'yonghu','email',"email='{$email}'");

			$emailrule = "/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/";

			if($query){

				$eun = 1 ;
			}else{
				$eun = 0 ;
			}

			

			$list['erule'] = preg_match($emailrule,$email);

			$list['euniqueness'] = $eun;	

			echo json_encode($list);die;

		}else if(isset($_POST['submit'])){
			$spassword = htmlspecialchars(trim($_POST['spassword'],''));

			$data['username'] = htmlspecialchars(trim($_POST['susername'],''));
			$data['nicheng'] = htmlspecialchars(trim($_POST['snicheng'],''));
			$data['password'] = crypt($spassword,md5($spassword));
			$data['phone'] = htmlspecialchars(trim($_POST['sphone'],''));
			$data['email'] = htmlspecialchars(trim($_POST['semail'],''));
			$data['per_id'] = $_POST['spermission'];
			$data['sex']  = $_POST['ssex'];
			$data['create_time'] = time();
			$admin_ins = dbinsert($con,'yonghu',$data);


			if($admin_ins){
				echo 1;die;
			}else{
				echo 0;die;
			}

		}

	}


	

	


?>